package org.benjamin.lending.trusteeship.sumapay;

import org.benjamin.lending.domain.entity.*;
import org.benjamin.lending.service.WithdrawService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;

import java.math.BigDecimal;
import java.util.Map;


/**
 * TODO 未完成
 */
@Scope(WebApplicationContext.SCOPE_REQUEST)
@Service("sumaplayUserWithdrawOperation")
public class UserWithdrawOperation extends SumapayAbstractOperation {

    private WithdrawService withdrawService;


    @Override
    protected void processRequest(Map<String, Object> requestMap, MemberInfo memberInfo, BorrowInfo borrowInfo, Map<String, Object> parameterMap) {
        BigDecimal amount = new BigDecimal(parameterMap.get("amount").toString());
        requestMap.put("sum",amount);
        requestMap.put("payType",2);
        //requestMap.put("")
    }

    @Override
    protected void afterRequest(Map<String, Object> parameterMap, MemberInfo memberInfo,BorrowInfo borrowInfo, TrusteeshipRequest trusteeshipRequest) {
        WithdrawLog withdrawLog = this.withdrawService.createWithdrawLog();
        trusteeshipRequest.setType(TrusteeshipType.withdraw);
        trusteeshipRequest.setRel(withdrawLog.getId());
        trusteeshipRequest.setTid(1);
    }

    /**
     *
     * @param requestMap
     * @return
     */
    @Override
    protected String signatureRequest(Map<String, Object> requestMap) {
        String[] signatures = this.buildSignature(requestMap,"requestId","merchantCode","userIdIdentity","sum","successResultUrl","failResultUrl","payType","subledgerList");
        requestMap.put("signature",signatures[1]);
        return signatures[0];
    }

    @Override
    protected String[] signatureResponse(Map<String, Object> responseMap) {
        String[] signatures = this.buildSignature(responseMap,"requestId","result","sum","userIdIdentity","userBalance");
        return signatures;
    }


}
